package notesDetection.fftPackage;

/**
 * FFT package was used for DEBUG, and is not part of the project
 * Supplies a DFT algorithm with asymptotic run time of O(n*log(n)) 
 * 
 * @author http://introcs.cs.princeton.edu/java/97data/FFT.java.html
 */

public class FFT {

    // compute the FFT of x[], assuming its length is a power of 2
    public static FftComplex[] fft(FftComplex[] x) {
        int N = x.length;

        // base case
        if (N == 1) return new FftComplex[] { x[0] };

        // radix 2 Cooley-Tukey FFT
        if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }

        // fft of even terms
        FftComplex[] even = new FftComplex[N/2];
        for (int k = 0; k < N/2; k++) {
            even[k] = x[2*k];
        }
        FftComplex[] q = fft(even);

        // fft of odd terms
        FftComplex[] odd  = even;  // reuse the array
        for (int k = 0; k < N/2; k++) {
            odd[k] = x[2*k + 1];
        }
        FftComplex[] r = fft(odd);

        // combine
        FftComplex[] y = new FftComplex[N];
        for (int k = 0; k < N/2; k++) {
            double kth = -2 * k * Math.PI / N;
            FftComplex wk = new FftComplex(Math.cos(kth), Math.sin(kth));
            y[k]       = q[k].plus(wk.times(r[k]));
            y[k + N/2] = q[k].minus(wk.times(r[k]));
        }
        return y;
    }

    public static FftComplex[] fft(double[] realArray, int fftResolution)
    {
    	return fft(FftComplex.GenerateComplexArray(realArray, fftResolution));
    }

    // compute the inverse FFT of x[], assuming its length is a power of 2
    public static FftComplex[] ifft(FftComplex[] x) {
        int N = x.length;
        FftComplex[] y = new FftComplex[N];

        // take conjugate
        for (int i = 0; i < N; i++) {
            y[i] = x[i].conjugate();
        }

        // compute forward FFT
        y = fft(y);

        // take conjugate again
        for (int i = 0; i < N; i++) {
            y[i] = y[i].conjugate();
        }

        // divide by N
        for (int i = 0; i < N; i++) {
            y[i] = y[i].times(1.0 / N);
        }

        return y;

    }

    // compute the circular convolution of x and y
    public static FftComplex[] cconvolve(FftComplex[] x, FftComplex[] y) {

        // should probably pad x and y with 0s so that they have same length
        // and are powers of 2
        if (x.length != y.length) { throw new RuntimeException("Dimensions don't agree"); }

        int N = x.length;

        // compute FFT of each sequence
        FftComplex[] a = fft(x);
        FftComplex[] b = fft(y);

        // point-wise multiply
        FftComplex[] c = new FftComplex[N];
        for (int i = 0; i < N; i++) {
            c[i] = a[i].times(b[i]);
        }

        // compute inverse FFT
        return ifft(c);
    }


    // compute the linear convolution of x and y
    public static FftComplex[] convolve(FftComplex[] x, FftComplex[] y) {
        FftComplex ZERO = new FftComplex(0, 0);

        FftComplex[] a = new FftComplex[2*x.length];
        for (int i = 0;        i <   x.length; i++) a[i] = x[i];
        for (int i = x.length; i < 2*x.length; i++) a[i] = ZERO;

        FftComplex[] b = new FftComplex[2*y.length];
        for (int i = 0;        i <   y.length; i++) b[i] = y[i];
        for (int i = y.length; i < 2*y.length; i++) b[i] = ZERO;

        return cconvolve(a, b);
    }

    // display an array of FftComplex numbers to standard output
    public static void show(FftComplex[] x, String title) {
        System.out.println(title);
        System.out.println("-------------------");
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i]);
        }
        System.out.println();
    }
}
